package View;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.FrameLayout;
import android.widget.ListView;

public class TouchMoveAction extends ListView implements
		AbsListView.OnScrollListener {
	private int mScreenHeight;
	private FrameLayout mHeaderContainer;
	private View mHeaderView;
	private int mHeaderHeight;
	private AbsListView.OnScrollListener mOnScrollListener;
	private View mShadow;
	private int mActivePointerId = -1;
	float mLastMotionY = -1.0F;
	float mLastScale = -1.0F;
	float mMaxScale = -1.0F;
	
	public TouchMoveAction(Context context) {
		// TODO Auto-generated constructor stub
		super(context);
		init(context);
	}

	public TouchMoveAction(Context paramContext, AttributeSet paramAttributeSet) {

		super(paramContext, paramAttributeSet);
		init(paramContext);

	}

	public TouchMoveAction(Context paramContext,
			AttributeSet paramAttributeSet, int paramInt) {

		super(paramContext, paramAttributeSet, paramInt);
		init(paramContext);

	}

	private void init(Context context) {
		DisplayMetrics localDisplayMetrics = new DisplayMetrics();
		((Activity) context).getWindowManager().getDefaultDisplay()
				.getMetrics(localDisplayMetrics);
		this.mScreenHeight = localDisplayMetrics.heightPixels;
		this.mHeaderContainer = new FrameLayout(context);
		this.mHeaderView = new View(context);
		int i = localDisplayMetrics.widthPixels;
		Log.v(">>>>>>>>>>>>>>>>>>>>>>>>>>>>", ">>>>>>>>>>>>>>>>>>>>>>>>>>>>");
		//屏幕宽分为16份 头view为屏幕的9倍
		Log.v("init", ""+ (int) ( i ));
		Log.v("init", ""+ (int) (9.0F * (i / 16.0F)));
		setHeaderViewSize(i, (int) (9.0F * (i / 16.0F)));
		this.mShadow=new View(context);
		FrameLayout.LayoutParams localLayoutParams=new FrameLayout.LayoutParams(-1,-2);
		localLayoutParams.gravity = Gravity.CENTER;
		this.mShadow.setLayoutParams(localLayoutParams);
		this.mHeaderContainer.addView(this.mHeaderView);
		this.mHeaderContainer.addView(this.mShadow);
		addHeaderView(this.mHeaderContainer);
		super.setOnScrollListener(this);
	}
	
	
	private void onSecondaryPointerUp(MotionEvent paramMotionEvent){
		int i=(paramMotionEvent.getAction())>>8;
		if (paramMotionEvent.getPointerId(i)==this.mActivePointerId) {
			if (i != 0) {
				int j = 1;
				this.mLastMotionY = paramMotionEvent.getY(0);
				this.mActivePointerId = paramMotionEvent.getPointerId(0);
				return;
			}
		}
	}
	
	private void reset() {
		this.mActivePointerId = -1;
		this.mLastMotionY = -1.0F;
		this.mMaxScale = -1.0F;
		this.mLastScale = -1.0F;
	}
	
	public View getHeaderView() {
		return this.mHeaderView;
	}
	
	public boolean onInterceptTouchEvent(MotionEvent paramMotionEvent) {
		return super.onInterceptTouchEvent(paramMotionEvent);
	}
	
	protected void onLayout(boolean paramBoolean, int paramInt1, int paramInt2,
			int paramInt3, int paramInt4) {
		super.onLayout(paramBoolean, paramInt1, paramInt2, paramInt3, paramInt4);
		if (this.mHeaderHeight == 0)
			this.mHeaderHeight = this.mHeaderContainer.getHeight();
	}
	
	public void setHeaderViewSize(int paramInt1, int paramInt2){
		Object localoObject=this.mHeaderContainer.getLayoutParams();
		if (localoObject==null) {
			localoObject=new AbsListView.LayoutParams(paramInt1,paramInt2);
			((ViewGroup.LayoutParams) localoObject).width = paramInt1;
			((ViewGroup.LayoutParams) localoObject).height = paramInt2;
			this.mHeaderContainer.setLayoutParams((ViewGroup.LayoutParams) localoObject);
			this.mHeaderHeight = paramInt2;
		}
	}

	public void setOnScrollListener(
			AbsListView.OnScrollListener paramOnScrollListener) {
		this.mOnScrollListener = paramOnScrollListener;
	}
	
	public void setShadow(int paramInt) {
		//this.mShadow.setBackgroundResource(paramInt);
	}

	
	@Override
	public void onScroll(AbsListView paramAbsListView, int paramInt1, int paramInt2, int paramInt3) {
		// TODO Auto-generated method stub
		//Log.d("mmm", "onScroll");
		float f = this.mHeaderHeight - this.mHeaderContainer.getBottom();
		//Log.d("mmm", "f|" + f);
		if ((f > 0.0F) && (f < this.mHeaderHeight)) {
		//	Log.d("mmm", "1");
			int i = (int) (0.65D * f);
			this.mHeaderView.scrollTo(0, -i);
		} else if (this.mHeaderView.getScrollY() != 0) {
		//	Log.d("mmm", "2");
			this.mHeaderView.scrollTo(0, 0);
		}
		if (this.mOnScrollListener != null) {
			this.mOnScrollListener.onScroll(paramAbsListView, paramInt1,
					paramInt2, paramInt3);
		}
	}

	@Override
	public void onScrollStateChanged(AbsListView paramAbsListView, int paramInt) {
		// TODO Auto-generated method stub
		if (this.mOnScrollListener != null)
			this.mOnScrollListener.onScrollStateChanged(paramAbsListView,
					paramInt);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		// TODO Auto-generated method stub
		Log.d("onTouchEvent", "" + ( ev.getAction()));

		switch (ev.getAction() & MotionEvent.ACTION_MASK) {
		case MotionEvent.ACTION_OUTSIDE:
		case MotionEvent.ACTION_DOWN:
//			if (!this.mScalingRunnalable.mIsFinished) {
//				this.mScalingRunnalable.abortAnimation();
//			}
			this.mLastMotionY = ev.getY();
			this.mActivePointerId = ev.getPointerId(0);
			Log.v("触摸的点", ""+this.mActivePointerId);
			//头部view占据屏幕多大?
			this.mMaxScale = (this.mScreenHeight / this.mHeaderHeight);
			//头部viewgroup占据屏幕多大
			this.mLastScale = (this.mHeaderContainer.getBottom() / this.mHeaderHeight);
			break;
		case MotionEvent.ACTION_MOVE:
			Log.d("mmm", "mActivePointerId" + mActivePointerId);
//			多点触碰时候选择第一个点
			int j = ev.findPointerIndex(this.mActivePointerId);
			if (j == -1) {
				Log.e("PullToZoomListView", "Invalid pointerId="
						+ this.mActivePointerId + " in onTouchEvent");
			} else {
				if (this.mLastMotionY == -1.0F)
					this.mLastMotionY = ev.getY(j);
				if (this.mHeaderContainer.getBottom() >= this.mHeaderHeight) {
					ViewGroup.LayoutParams localLayoutParams = this.mHeaderContainer
							.getLayoutParams();
					float f = ((ev.getY(j) - this.mLastMotionY + this.mHeaderContainer
							.getBottom()) / this.mHeaderHeight - this.mLastScale)
							/ 2.0F + this.mLastScale;
					if ((this.mLastScale <= 1.0D) && (f < this.mLastScale)) {
						localLayoutParams.height = this.mHeaderHeight;
						this.mHeaderContainer
								.setLayoutParams(localLayoutParams);
						return super.onTouchEvent(ev);
					}
					this.mLastScale = Math.min(Math.max(f, 1.0F),
							this.mMaxScale);
					localLayoutParams.height = ((int) (this.mHeaderHeight * this.mLastScale));
					if (localLayoutParams.height < this.mScreenHeight)
						this.mHeaderContainer
								.setLayoutParams(localLayoutParams);
					this.mLastMotionY = ev.getY(j);
					return true;
				}
				this.mLastMotionY = ev.getY(j);
			}
			break;
		case MotionEvent.ACTION_UP:
			reset();
			endScraling();
			break;
		case MotionEvent.ACTION_CANCEL:
			int i = ev.getActionIndex();
			this.mLastMotionY = ev.getY(i);
			this.mActivePointerId = ev.getPointerId(i);
			break;
		case MotionEvent.ACTION_POINTER_DOWN:
			onSecondaryPointerUp(ev);
			this.mLastMotionY = ev.getY(ev
					.findPointerIndex(this.mActivePointerId));
			break;
		case MotionEvent.ACTION_POINTER_UP:
		}
		return super.onTouchEvent(ev);
	}
	private void endScraling() {
		if (this.mHeaderContainer.getBottom() >= this.mHeaderHeight)
			Log.d("mmm", "endScraling");
	//	this.mScalingRunnalable.startAnimation(200L);
	}
}
